/* eslint-disable */
// @ts-check

/**
 * 根据 path 过滤接口
 * @param {string} path
 * @param {string|RegExp} rule
 */
const matchPath = (path, rule) => {
  return path === rule;
}

/**
 * 根据 path 过滤接口
 * @param {import("auto-service/lib/consts").SwaggerJson['paths']} paths
 * @param {readonly (string|RegExp)[]} rules
 * @returns {import("auto-service/lib/consts").SwaggerJson['paths']}
 */
exports.filterPaths = (paths, rules) => {
  /** @type {typeof paths} */
  const result = {};

  for (const [path, data] of Object.entries(paths)) {
    const matched = rules.some(rule => matchPath(path, rule));
    if (matched) {
      result[path] = data;
    }
  }

  return result;
};

/**
 * 过滤 definitions
 * @param {import("auto-service/lib/consts").SwaggerJson['definitions']} definitions
 * @param {readonly string[]} names
 * @returns {import("auto-service/lib/consts").SwaggerJson['definitions']}
 */
exports.filterDefinitions = (definitions, names) => {
  if (!definitions) { return {}; }

  /** @type {typeof definitions} */
  const result = {};
  for (const [name, data] of Object.entries(definitions)) {
    if (names.includes(name)) {
      result[name] = data;
    }
  }

  return result;
};
